Polymorphism
Get familiar with the concept of polymorphism and its types with implementation.
Introduction to polymorphism#
The word polymorphism is a combination of two Greek words, “poly” meaning many, and “morph” meaning forms. In programming, polymorphism is a phenomenon that allows an object to have several different forms and behaviors.
For example, take the Animal
class. There are many different animals, e.g., lion, deer, dog, and crocodile, etc. So, they are all animals, but their properties are different. The animal class can have a method, makeNoise
. Its implementation should be different for a lion, deer, or any other animal as they all have different noises. This is called polymorphism.
Types of polymorphism#
There are two types of polymorphism: dynamic polymorphism and static polymorphism, as shown in the figure below.
Dynamic polymorphism#
Dynamic polymorphism is the mechanism that defines the methods with the same name, return type, and parameters in the base class and derived classes. Hence, the call to an overridden method is decided at runtime. That is why dynamic polymorphism is also known as runtime polymorphism. It is achieved by method overriding.
Method overriding #
In object-oriented programming, if a subclass provides a specific implementation of a method that had already been defined in one of its parent classes, it is known as method overriding.
Suppose we have a parent class, Animal
, with its subclass, Lion
. Below is the implementation of two functions with the same name in each class to check method overriding behavior.
Static polymorphism#
Static polymorphism is also known as compile-time polymorphism, and it is achieved by method overloading or operator overloading.
Method overloading#
Methods are said to be overloaded if a class has more than one method with the same name, but either the number of arguments is different, or the type of arguments is different. We have implemented method overloading using two functions with the same name but with different numbers of arguments. You can see this in the implementation below.
Operator overloading#
Operators can be overloaded to operate in a certain user-defined way. Its corresponding method is invoked to perform its predefined function whenever an operator is used. For example, when the +
operator is called, it invokes the special function, add
, but this operator acts differently for different data types. The +
operator adds the numbers when it is used between two int
data types and merges two strings when used between string
data types.
Let’s look at the implementation below, where we’ve overloaded the +
operator to add complex numbers instead of simply adding two real numbers.
Note: Java and JavaScript do not support operator overloading.
Dynamic polymorphism vs. static polymorphism#
The table below provides a highlight of the differences between dynamic and static polymorphism:
Static Polymorphism | Dynamic Polymorphism |
Polymorphism that is resolved during compile-time is known as static polymorphism. | Polymorphism that is resolved during runtime is known as dynamic polymorphism. |
Method overloading is used in static polymorphism. | Method overriding is used in dynamic polymorphism. |
Mostly used to increase the readability of the code. | Mostly used to have a separate implementation for a method that is already defined in the base class. |
Arguments must be different in the case of overloading. | Arguments must be the same in the case of overriding. |
Return type of the method does not matter. | Return type of the method must be the same. |
Private and sealed methods can be overloaded. | Private and sealed methods cannot be overridden. |
Gives better performance because the binding is being done at compile-time. | Gives worse performance because the binding is being done at runtime. |
Let's test our OOP concepts in the next lesson.
Inheritance
Quiz: Object-oriented Basics